home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Languguage OS 2
/
Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO
/
language
/
embedded
/
simulato
/
v2_3_mc6.tz
/
v2_3_mc6
/
testfiles
/
treefnd2.asm
< prev
next >
Wrap
Assembly Source File
|
1994-05-02
|
16KB
|
301 lines
;
;
; Assn 3: Binary Search Tree.
; 11/18/92
;
; --------------------
;
; This program uses a binary tree implementation. It will accept user commands
; to insert an element on the tree, search the tree for an element, print the
; tree in an inorder traversal, or exit the program. The commands are
; I - insert digit in list, S - search for element in list, P - print list,
; and E - exit. Elements are single digit numbers (0-9).
;
; Note that this accepts one digit numbers as the data for each node of the
; tree, even though a word size of space is allocated.
;
; a6 = anchor pointer of the tree
; d7 = user data field
; d0 = user command
;
; this program uses extensive subroutine functions.
;
; --------------------
;
org $1000 ; program origin
left equ 0 ; offset for right node pointer
data equ 4 ; offset for data region of node
right equ 6 ; offset for left node pointer
clr.l d7 ; zero d7, data field
move.l d7,a6 ; zero a6, anchor pointer
move.w #$3030,d7 ; set low d7 to two ASCII "0" chars
jsr ClearScreen ; clear da screen
main move #prompt,a0 ; set a0 to point to prompt message
jsr WriteString ; display prompt
move.l #userdata,a0 ; set a0 to point to storage area
jsr ReadString ; get user input
move.l #userdata,a0 ; restore the a0 pointer
clr.l d0
7$ cmpi.b #$20,(a0) ; is this char a space?
bne 8$ ; yes! parse the space!
addq #1,a0 ; increment a0 by 1 byte
bra 7$ ; parse loop repeat
8$ move.b (a0),d0 ; store first char in d0
addq #1,a0 ; incr a0 to next byte
cmpi.b #'P',d0 ; is d0 = 'P' for PRINT?
beq 1$ ; YES! Skip to 1$
cmpi.b #'E',d0 ; is d0 = 'E' for EXIT?
beq exit ; YES! Exit program
cmpi.b #'S',d0 ; is d0 = 'S' for SEARCH?
beq 2$ ; YES! skip to 2$
cmpi.b #'I',d0 ; is d0 = 'I' for INSERT?
beq 3$ ; YES! Skip to 3$
move.l #entryerror,a0 ; set a0 to error message
jsr WriteString ; display it!
bra main ; repeat loop for user commands!
1$ jsr WriteEOL ; end of line
jsr ClearScreen ; go clear the screen
jsr PrintTree ; go print the tree
bra main ; repeat loop for user commands
2$ cmpi.b #$20,(a0) ; is this char a space?
bne 21$ ; no! skip ahead!
addq #1,a0 ; up a0 to next char
bra 2$ ; parse space loop
21$ move.b (a0),d7 ; put only digit in low byte d7
jsr WriteEOL
jsr SearchTree ; go search for an occurance of d7
bra main ; repeat loop for user commands
3$ cmpi.b #$20,(a0) ; is this char a space?
bne 31$ ; no! skip to 31$
addq #1,a0 ; up a0 to next char
bra 3$ ; do parse loop on spaces
31$ move.b (a0),d7 ; put only digit in lo byte d7
jsr WriteEOL
jsr InsertNode ; go try to insert data to tree
bra main ; repeat loop for user commands
exit move #228,d7 ; store exit code in d7
trap #14 ; stop program execution/priv error
NOLIST ;No list turns of listing so you only get your code.
DC.W 0 ;Forces a Word Boundary.
Include "samples.asm" ;Includes our routines in your program.
LIST ;Turns listing back on for you
DC.W 0
;
; SubRoutine GetNewNode : It calls malloc and obtains a 10 byte (defined in d0)
; block of memory for the next node. It also zeros the pointers and the
; data are of the new node block. a0 contains the address of the new
; block, and is the only register changed.
;
GetNewNode equ * ; entry point for subroutine
clr.l d0 ; zero d0
move.b #10,d0 ; set up for 10 bytes of memory
jsr malloc ; go get 10byte block
clr.l right(a0) ; clear right pointer of block
clr.w data(a0) ; clear data area of block
clr.l left(a0) ; clear left pointer of block
rts ; return to calling code
;
; SubRoutine InsertNode : Insert Node will parse the tree and try to place in
; the appropriate spot the user data. If the data already exists, it
; will tell the user as much, and then exit. Otherwise, it will tell
; the user it successfully inserted the data.
;
; This expects a6 to point to the anchor node of the tree,
; d7 to contain the user data, and will not corrupt any registers,
; with the possible exception of a6 if the tree is empty.
;
InsertNode equ * ; entry point for subroutine
move.l a6,a5 ; make a working copy of the anchor pointer
move.l a6,d1 ; dupe a6 to d1
cmpi.l #0,d1 ; is the anchor = NIL?
bne 1$ ; NO! Skip ahead in code!
jsr GetNewNode ; YES! Tree empty! Get first node!
move.w d7,data(a0) ; Store our value in data region of a0
move.l a0,a6 ; set anchor to point to this block
jmp 5$ ; Exit AddNode! (done!)
1$ move.w data(a5),d1 ; dupe data to d1
cmp d7,d1 ; test our data to current node data
bgt 3$ ; our data is less than! Skip ahead
blt 4$ ; our data is greater than! Skip ahead
; Our data is EQUAL! Do this!
move.w d7,source ; store our data in source field
move.l #opener,a0 ; set a0 to point to opener message
jsr WriteString ; go print opener text
move.l #dup,a0 ; set a0 to point to duplicate data error
jsr WriteString ; go display that!
move.l #waiting,a0 ; set a0 to point to "press a key" message
jsr WriteString ; put that on the screen
jsr ReadChar ; Wait for user response
jmp Exit_AN ; exit code
3$ tst.l left(a5) ; can we move left in tree?
beq 31$ ; NO! Insert a node here! (skip ahead)
move.l left(a5),a5 ; Yes! We can go left! So set a5!
jmp 1$ ; Repeat Loop!
31$ jsr GetNewNode ; Grab a new node!
move.w d7,data(a0) ; store our data in node!
move.l a0,left(a5) ; set this node's left pointer to our new node!
jmp 5$ ; jump and display inserted message!
4$ tst.l right(a5) ; Can we move right in the tree?
beq 41$ ; NO! Insert a node here! (skip ahead)
move.l right(a5),a5 ; YES! We can go right! Set a5!
jmp 1$ ; Repeat Loop!
41$ jsr GetNewNode ; Grab a new node!
move.w d7,data(a0) ; store our data in node!
move.l a0,right(a5) ; set this node's right pointer to our new node!
5$ move.w d7,source ; store our data at source location
move.l #opener,a0 ; set a0 to opener message
jsr WriteString ; display that!
move.l #inserted,a0 ; set a0 to display inserted message
jsr WriteString ; display that!
move.l #waiting,a0 ; set a0 to point to "press a key" message
jsr WriteString ; display that!
jsr ReadChar ; wait for user response!
Exit_AN rts ; return to calling code
;
; SubRoutine SearchTree : This routien will search the tree for an occurance
; of the user data. It expects a6 to be the anchor pointer, and d7 to
; be the user data. It corrupts no registers, and displays appropriate
; "found" or "not found" result codes on the monitor.
;
SearchTree equ * ; entry point for code
movem.l a5,-(a7) ; store a5, register used
move.l a6,a5 ; make a5 working copy of anchor
move.w d7,source ; store d7 in source field
move.l a6,d1 ; dupe a6 to d1 for comp
cmpi.l #0,d1 ; is tree empty?
beq 4$ ; yes! exit code and give error!
1$ move.w data(a5),d1 ; move data t od1
cmp.w d7,d1 ; check our data against node data
bgt 2$ ; less than? YES! skip ahead!
blt 3$ ; greater than? YES! skip ahead!
move.l #opener,a0 ; set a0 to point to opener msg
jsr WriteString ; display that!
move.l #wasfound,a0 ; set a0 to "found" message
jsr WriteString ; display that!
move.l #waiting,a0 ; set a0 to "press a key" msg
jsr WriteString ; display that
jsr ReadChar ; wait for user response
bra Exit_ST ; exit this code!
2$ tst.l left(a5) ; can we go left in tree?
beq 4$ ; NO! Exit code and give error!
move.l left(a5),a5 ; set a5 to left node!
bra 1$ ; repeat loop!
3$ tst.l right(a5) ; can we go right in tree?
beq 4$ ; No! Exit code and give error!
move.l right(a5),a5 ; set a5 to right node!
bra 1$ ; repeat loop!
4$ move.l #opener,a0 ; set a0 to opener msg
jsr WriteString ; display that!
move.l #notfound,a0 ; set a0 to "not found" message
jsr WriteString ; display that!
move.l #waiting,a0 ; set a0 to "press a key" message
jsr WriteString ; display that!
jsr ReadChar ; wait for user input
bra Exit_ST ; exit code!
Exit_ST movem.l (a7)+,a5 ; restore a5
rts ; return to calling code
;
; SubRoutine PrintTree : This code will parse the tree and print in-order
; traversal results of the binary tree, or an error message saying
; the tree is empty if a6 = null. This code corrupts no registers,
; and implements stack procedures via a7 postincr/predecr calls.
; It only expects a6 to be the anchor pointer
;
PrintTree equ * ; entry point for code
move.l a6,a5 ; make a5 working copy of anchor
move.l a6,d1 ; dupe a6 to d1 for comp
cmpi.l #0,d1 ; is tree empty?
beq 4$ ; yes! exit code and give error!
move.l #0,-(a7) ; put a null in the stack longword sized
1$ move data(a5),source ; copy this node's data to disp field
move.l #source,a0 ; set a0 to point to disp field
jsr WriteString ; display that!
tst.l right(a5) ; can we go right?
beq 2$ ; NO! Skip to 2$
move.l right(a5),-(a7) ; push right address to stack
2$ tst.l left(a5) ; can we go left?
beq 3$ ; No! Skip to 3$
move.l left(a5),a5 ; set a5 to left node
bra 1$ ; repeat loop!
3$ move.l (a7)+,a5 ; pop the next "right" address off stack
move.l a5,d1 ; dupe a5 to d1
cmpi.l #0,d1 ; is a5 = 0 (done) ?
beq Exit_PT ; YES! Exit this code!
bra 1$ ; No! repeat loop!
4$ move.l #emptytree,a0 ; set a0 to "empty tree" message
jsr WriteString ; display that!
move.l #waiting,a0 ; set a0 to "press a key" message
jsr WriteString ; display that!
jsr ReadChar ; wait for user input
Exit_PT jsr WriteEOL ; put an eol marker on screen
rts ; return to calling code
;
; User messages follow:
;
opener dc.b 'Integer '
source ds.w 1
dc.b ' ',0
dup dc.b 'already exists in the tree.',13,10,0
inserted dc.b 'was inserted successfully.',13,10,0
notfound dc.b 'was not found.',13,10,0
wasfound dc.b 'was found in the tree.',13,10,0
waiting dc.b 'Press any key to continue, please...',13,10,0
emptytree dc.b 'Not possible to print tree: Tree is empty.',13,10,0
prompt dc.b 'Command? > ',0
entryerror dc.b 'Error: Unable to understand. Use "I", "S", "P", or "E".',13,10
dc.b ' I 5 would insert 5 into the tree;',13,10
dc.b ' S 5 would search for 5 in the tree;',13,10
dc.b ' P would PRE-ORDER print the tree;',13,10
dc.b ' E would exit this program.',13,10,13,10,0
userdata ds.w 6
;
; now end source listing
;
end